#delimit;
clear all;
set more off;

import delimited "MEDSL_PrecinctResults_ByMode_2018.csv", clear;


egen num_votes_cast_gen = max(total_total), by (county_name precinct);


tab office;

drop if office == "US HOUSE";


gen kemp_vs_gen = republican_total/(republican_total + democrat_total);


keep kemp_vs_gen num_votes_cast_gen county_name precinct;

rename county_name county;

merge 1:1 county precinct using "Runoff2018_PrecResults.dta";

drop _merge;


gen delta_votes_cast = num_votes_cast_ro - num_votes_cast_gen;
gen delta_votes_cast_perc =  delta_votes_cast/num_votes_cast_gen;



lowess delta_votes_cast_perc kemp_vs_gen, gen(delta_votes_hat);



twoway  (scatter delta_votes_cast_perc kemp_vs_gen, msize(tiny) msymbol(oh)) 
(lpolyci delta_votes_hat kemp_vs_gen,  ciplot(rline)) ,
title(" ") ytitle("{&Delta} in Precinct Turnout" "between Runoff and General") xtitle("Kemp Vote Share in 2018 General")  graphregion(color(white)) bgcolor(white)
legend(off) saving(2018_Precinct_Scatter.gph, replace); 




 
#delimit;

import delimited "MEDSL_PrecinctResults_ByMode_2022.csv", clear;


drop if stage == "GEN RUNOFF";

egen num_votes_cast_gen = max(total_total), by(county_name precinct);


gen kemp_vs_gen_h = republican_total/(republican_total + democrat_total) if office == "GOVERNOR";


egen kemp_vs_gen = mean(kemp_vs_gen_h), by(county_name precinct);


 
gen walker_vs_gen_h = republican_total/(republican_total + democrat_total) if office == "US SENATE";


egen walker_vs_gen = mean(walker_vs_gen_h), by(county_name precinct);



tab office;


keep if office == "GOVERNOR";


keep kemp_vs_gen walker_vs_gen num_votes_cast_gen county_name precinct;


rename county_name county;


merge 1:1 county precinct using "Runoff2022_PrecResults.dta";


*GA SOS reports no votes for City of Baldwin Habersham County in runoff;  
*GA SOS reports no votes for Savannah precincts within Chatham County in runoff;  
*Dropping these from analysis;

tab _merge;

gen num_votes_cast_ro_alt = num_votes_cast_ro;
replace num_votes_cast_ro_alt = 0 if _merge == 2;

drop _merge;

gen delta_votes_cast = num_votes_cast_ro - num_votes_cast_gen;
gen delta_votes_cast_perc =  delta_votes_cast/num_votes_cast_gen;



lowess delta_votes_cast_perc kemp_vs_gen, gen(delta_votes_hat);


twoway  (scatter delta_votes_cast_perc kemp_vs_gen if delta_votes_cast_perc > -0.5 & delta_votes_cast_perc < 0.05, msize(tiny) msymbol(oh)) 
(lpolyci delta_votes_hat kemp_vs_gen if delta_votes_cast_perc > -0.5 & delta_votes_cast_perc < 0.05,  ciplot(rline)) ,
title(" ") ytitle("{&Delta} in Precinct Turnout" "between Runoff and General") xtitle("Kemp Vote Share in 2022 General") graphregion(color(white)) bgcolor(white)
legend(off) saving(2022_Precinct_Scatter.gph); 




gr combine 2018_Precinct_Scatter.gph 2022_Precinct_Scatter.gph;


graph export "2018_2022_Precinct_Scatter.pdf", replace;

